package com.generalbackend.role.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generalbackend.role.mapper.RolePermissionMapper;
import com.generalbackend.role.model.entity.RolePermission;
import com.generalbackend.role.model.query.RolePermissionQuery;
import com.generalbackend.role.service.IRolePermissionService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.generalbackend.common.constants.Constants.DATA;
import static com.generalbackend.common.constants.Constants.TOTAL;

/**
* @Description 角色权限关联表业务层实现类
* @author tim.xie
* @date 2021-12-16 11:11
*/

@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    private Logger LOG = LoggerFactory.getLogger(RolePermissionServiceImpl.class);

	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Override
	public Boolean insert(RolePermission rolePermission) {
        // 新增校验
        checkRolePermission(rolePermission);
        // 填充额外属性
        fillRolePermission(rolePermission);
        Integer num = rolePermissionMapper.insert(rolePermission);
        if(num <= 0) {
         return false;
         }
		return true;
	}

	@Override
	public Boolean update(RolePermission rolePermission) {
        // 修改校验
        checkRolePermission(rolePermission);
        // 填充额外属性
        fillRolePermission(rolePermission);
        Integer num = rolePermissionMapper.updateById(rolePermission);
        if(num <= 0) {
          return false;
        }
		return true;
	}
    /**
    * 新增或者修改校验
    * @param rolePermission
    * @return
    */
    private void checkRolePermission(RolePermission rolePermission) {

    }
    /**
    *填充额外属性
    **/
    private void fillRolePermission(RolePermission rolePermission) {

    }

	@Override
	public Boolean deleteById(Long id) {
        rolePermissionMapper.deleteById(id);
		return true;
	}

	@Override
	public RolePermission selectById(Long id) {
        RolePermission rolePermission = rolePermissionMapper.selectById(id);
        return rolePermission;
	}
    @Override
    public JSONObject list(RolePermissionQuery rolePermissionQuery) {
        JSONObject result = new JSONObject();
        Page<RolePermission> page = new Page<>(rolePermissionQuery.getOffset(), rolePermissionQuery.getLimit());
        List<RolePermission> rolePermissionList = rolePermissionMapper.list(page,rolePermissionQuery);
        result.put(TOTAL,  page.getTotal());
        result.put(DATA, rolePermissionList);
        return result;
    }

    @Override
    public Boolean deleteRolePermissionByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<RolePermission>()
                .in(RolePermission::getRole, roleIds);
        return remove(queryWrapper);
    }

    @Override
    public boolean permissionUpdate(String add, String remove, Long roleId) {
        boolean result = false;
        if (StringUtils.isNotBlank(add)) {
            String[] permissionIds = add.split(",");
            if(null != permissionIds) {
                List<RolePermission> collect = Arrays.stream(permissionIds).map(permissionId -> {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.setRole(roleId);
                    rolePermission.setPermission(permissionId);
                    return rolePermission;
                }).collect(Collectors.toList());
                result = saveBatch(collect);
            }
        }
        if (StringUtils.isNotBlank(remove)) {
            String[] permissionIds = remove.split(",");
            if(null != permissionIds) {
                result = remove(new LambdaQueryWrapper<RolePermission>()
                        .eq(RolePermission::getRole,roleId)
                        .in(RolePermission::getPermission, permissionIds));
            }
        }
        return result;
    }

    @Override
    public boolean addRolePermission(List<String> permissions, Long roleId) {
        ArrayList<RolePermission> rolePermissions = new ArrayList<>();
        permissions.forEach(permission -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRole(roleId);
            rolePermission.setPermission(permission);
            rolePermissions.add(rolePermission);
        });
        saveBatch(rolePermissions);
        return true;
    }

    @Override
    public boolean deleteByRole(Long roleId) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRole, roleId);
        return remove(queryWrapper);
    }

    @Override
    public List<RolePermission> getByRoleId(Long roleId) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<RolePermission>()
                .eq(RolePermission::getRole, roleId);
        return rolePermissionMapper.selectList(queryWrapper);
    }


}
